package com.xuanyuanxinyu.core.listener;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;


import com.xuanyuanxinyu.core.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;



/**
* @FileName:LifecycleListener
* @Description: 生命周期监听
* @author ken.luo
* @date 2023/4/4
* @version V1.0
* @email 424533553@qq.com
*/
public class LifecycleListener implements LifecycleObserver {

    private static final String TAG = "EufyLifecycleListener";

    private List<SimpleAppLifecycleCallback> mCallbackList;

    public void addCallback(final SimpleAppLifecycleCallback callback) {
        if (mCallbackList == null) {
            mCallbackList = new ArrayList<>();
        }
        if (!mCallbackList.contains(callback)) {
            mCallbackList.add(callback);
        }
    }

    public void removeCallback(final SimpleAppLifecycleCallback callback) {
        if (mCallbackList == null || mCallbackList.isEmpty()) {
            return;
        }
        mCallbackList.remove(callback);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_CREATE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onMoveToForeground() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_START");
        if (mCallbackList != null) {
            for (SimpleAppLifecycleCallback callback : mCallbackList) {
                if (callback != null) {
                    callback.onMoveToForground();
                }
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_RESUME");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_PAUSE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onMoveToBackground() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_STOP");
        if (mCallbackList != null) {
            for (SimpleAppLifecycleCallback callback : mCallbackList) {
                if (callback != null) {
                    callback.onMoveToBackgroud();
                }
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        LogUtil.i(TAG, "Lifecycle.Event.ON_DESTROY");
    }

}
